#include "stdafx.h"
#include "ImageFileUtil.h"


ImageFileUtil::ImageFileUtil()
{
}


ImageFileUtil::~ImageFileUtil()
{
}


bool ImageFileUtil::WriteArrayToFile(IImageComputeFn *fn, int width, int height, const char *pathfilename, const char *arrayIntsStr)
{
	bool rc = false;

	if ((fn != nullptr) && (pathfilename != nullptr) && (arrayIntsStr != nullptr))
	{
		int cnt = 0;
		if ((cnt = fn->ArrayIntsGetSize(arrayIntsStr)) != IImageComputeFn::Invalid)
		{
			int *pts = new int[cnt];
			int *p = pts;
			if (fn->ArrayIntsConvertStr2Ints(arrayIntsStr, pts, cnt, IImageComputeFn::Invalid) == true)
			{
				Mat img_show(width, height, CV_8UC3, Scalar(0));

				vector<Point2f> epts(cnt/2);

				for (int x = 0; x < cnt/2; x++)
				{
					epts[x].x = (float) *p;
					p++;
					epts[x].y = (float) *p;
					p++;
				}
				drawPts(img_show, epts);
				if (imwrite(pathfilename, img_show) == true)
					rc = true;
			}
			delete[] pts;
		}
	}
	return rc;
}

void ImageFileUtil::drawPts(Mat& img, vector<Point2f>& pts)
{
	for (size_t i = 0; i < pts.size(); ++i)
	{
		circle(img, pts[i], 3, Scalar(0, 255, 0));
	}

	for (size_t i = 0; i < pts.size() - 1; ++i)
	{
		line(img, pts[i], pts[i + 1], Scalar(0, 0, 255));
	}
	line(img, pts.back(), pts.front(), Scalar(0, 0, 255));
}